package com.wxt.blog.service;

import com.wxt.blog.dao.UserDao;
import com.wxt.blog.entity.User;
import com.wxt.blog.util.MyBatisUtil;

public class UserService {

    public User login(String username, String password) {
        try {
            UserDao userDao = MyBatisUtil.getMapper(UserDao.class);
            User user = userDao.findByUsername(username);

            if (user != null && user.getPassword().equals(password)) {
                return user;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public boolean register(User user) {
        try {
            UserDao userDao = MyBatisUtil.getMapper(UserDao.class);
            if (userDao.findByUsername(user.getUsername()) != null) {
                return false;
            }
            userDao.insert(user);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}